package xin.nick.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.nick.constant.SystemConstants;
import xin.nick.domain.dto.SystemUserCreateDTO;
import xin.nick.domain.dto.SystemUserUpdateDTO;
import xin.nick.domain.query.SystemUserQuery;
import xin.nick.domain.vo.SystemUserVO;
import xin.nick.service.SystemUserService;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 系统用户信息 前端控制器
 * </p>
 *
 * @author Nick
 * @since 2023-06-23
 */
@Tag(name = "系统用户信息模块")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/systemUser")
public class SystemUserController {

    private final SystemUserService systemUserService;

    @Operation(summary = "系统用户信息详情",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @GetMapping("/getDetail/{id}")
    @PreAuthorize("@webSecurity.check('systemUser:detail')")
    public SystemUserVO getDetail(@PathVariable(name = "id") Long id) {
        return systemUserService.getDetail(id);
    }

    @Operation(summary = "系统用户信息列表",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @GetMapping("/getList")
    @PreAuthorize("@webSecurity.check('systemUser:list')")
    public List<SystemUserVO> getList(SystemUserQuery query) {
        return systemUserService.getList(query);
    }

    @Operation(summary = "系统用户信息列表分页",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @GetMapping("/getListPage")
    @PreAuthorize("@webSecurity.check('systemUser:list')")
    public Page<SystemUserVO> getListPage(SystemUserQuery query) {
        return systemUserService.getListPage(query);
    }

    @Operation(summary = "系统用户信息创建",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @PostMapping("/create")
    @PreAuthorize("@webSecurity.check('systemUser:add')")
    public SystemUserVO create(@RequestBody @Valid SystemUserCreateDTO dto) {
        return systemUserService.createSystemUser(dto);
    }

    @Operation(summary = "系统用户信息更新",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @PutMapping("/update")
    @PreAuthorize("@webSecurity.check('systemUser:update')")
    public SystemUserVO update(@RequestBody @Valid SystemUserUpdateDTO dto) {
        return systemUserService.updateSystemUser(dto);
    }

    @Operation(summary = "系统用户信息删除",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasRole('" + SystemConstants.ROOT + "') || hasAnyAuthority('systemUser:delete')")
    public void delete(@PathVariable(name = "id") Long id) {
        systemUserService.deleteById(id);
    }

    @Operation(summary = "系统用户信息批量删除",
            security = { @SecurityRequirement(name = SystemConstants.HEADER_AUTHORIZATION) })
    @DeleteMapping("/deleteByIdList/{idList}")
    @PreAuthorize("@webSecurity.check('systemUser:delete')")
    public void delete(@PathVariable(name = "idList") List<Long> idList) {
        systemUserService.deleteByIdList(idList);
    }

}
